INSERT INTO history
SELECT history_id_seq.NEXTVAL, (SELECT MAX(history_date) FROM history) + 30, h.quantity,h.device, 4 FROM history h, devices d
WHERE h.event = 2 AND d.device_id = h.device AND d.bandwidth <= 10;
INSERT INTO history
SELECT history_id_seq.NEXTVAL, (SELECT MAX(history_date) FROM history) + 30, h.quantity,h.device, 4 FROM history h, devices d
WHERE h.event = 3 AND d.device_id = h.device AND d.bandwidth <= 10;
INSERT INTO history
SELECT history_id_seq.NEXTVAL, (SELECT MAX(history_date) FROM history) + 30, h.quantity,h.device, 4 FROM history h, devices d
WHERE h.event = 4 AND d.device_id = h.device AND d.bandwidth <= 10;

